﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Collections;

namespace YehbaLogin
{
    public partial class ChatBox : Form
    {
        #region Controls and Variables

        //has something to do with easy drag and drop of form to any location even without form title bar
        private bool IsFormBeingDragged = false;
        private int MouseDownX;
        private int MouseDownY;
      
        //this variable contains the name of the new ChatBox window, it needs to be searched to the FormCollection
        //if this exists or not
        public static String name = String.Empty;

        //variable that says that an instance of a form is already added in FormsCollection
        public static bool flag = false;
        
        //varibale that says that a form is found in FormsCollection
        public static bool found = false;
        
        //create an instance of a form
        private static Form f = new Form();
        
        //create new region with rounded corners
        private NewRegion form_region;
        
        #endregion

        #region Constructor
        public ChatBox()
        {
            //initially flag is false
            flag = false;

            //check wheather FormsCollection is empty or not
            if (Yehba.Forms.Count == 0)
            {
                //if Count is zero (means no form is added yet)
                //add one
                Yehba.Forms.Add(this);
                //set flag to true
                flag = true;

            }
            else
            {
                //if FormsCollection is not empty
                //count is just a counter
                int count = 1;
                //Loop in FormsCollection
                foreach (Form LoopForm in Yehba.Forms)
                {
                    flag = false;
                    //Compare the names of forms in FormsCollection with suggested name
                    if (LoopForm.Name != name) //searching in form collection
                    {
                        //if counter has reached the last most index in FormsCollection
                        if (count == Yehba.Forms.Count)
                        {
                            Yehba.Forms.Add(this); //will add the form to the collection
                            //Add it to FormsCollection
                            flag = true;
                            break;
                        }
                        //increment count
                        count++;
                    }
                    else
                    {
                        flag = false;
                        //if found in FormsCollection
                        if (LoopForm.Name == name)
                        {
                            //call found object, then set to true
                            found = true;
                        }
                    }
                }
            }

            Point[] myArray =
             {
                
                 new Point(0, 9),
                 new Point(1,7),
                 new Point(2,5),
                 new Point(4,3),
                 new Point(8, 1),
                 new Point(9,1),    //upper left corner
               
                 new Point(427,0),
                 new Point(430,2),
                 new Point(432,3),
                 new Point(433,5),
                 new Point(434,7),
                 new Point(436,10),
                 new Point(437,12),  //upper right corner
              
                 new Point(437,274),
                 new Point(436,277),
                 new Point(435,280),
                 new Point(433,283),
                 new Point(430,285),
                 new Point(427,287),
                 new Point(424,288),  //lower right corner
                 
                 
                 new Point(11,288),
                 new Point(9,287),
                 new Point(7,286),
                 new Point(4,284),
                 new Point(2,282),
                 new Point(1,280),
                 new Point(0,278),   //lower left corner
             };

            //this creates form with rounded corners
            form_region = new NewRegion(myArray);
            this.Region = new Region(form_region.getGraphicsPath);
        }
        #endregion

        #region Methods

        /// <summary>
        /// Create instance of Chat window
        /// </summary>
        public void CreateChatBox()
        {

            InitializeComponent();
            f = this;
            //form text
            this.Text = name;
            //form name
            this.Name = name;
            //label control that contains name of contact
            this.User.Text = name;
            //create new form region
            this.Region = new Region(form_region.getGraphicsPath);
        }
        #endregion

        #region Form and Control Events

        //user moved this form up
        private void Yehba_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = false;
            }
        }
        
        //user moved this form down
        private void Yehba_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;
            }
        }

        //user moves this form
        private void Yehba_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormBeingDragged)
            {
                Point temp = new Point();
                temp.X = this.Location.X + (e.X - MouseDownX);
                temp.Y = this.Location.Y + (e.Y - MouseDownY);
                this.Location = temp;
                temp.X = 0;
                temp.Y = 0;

            }
        }

        //user clicked close
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //user clicked maximize
        private void btnMaximize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        //user clicked minimize
        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion
    }
}
